#include "setfilehandler.h"
#include <QDir>
SetFileHandler::SetFileHandler(QObject *parent) :
    QObject(parent)
{
    this->generate();
    settingsFile =  new QFile(SETTINGSPATH+"/settings");
    lastError="";
}

SetFileHandler* SetFileHandler::_instance = 0;

SetFileHandler* SetFileHandler::getInstance()
{
    if (_instance == 0) {
        _instance = new SetFileHandler;
    }
    return _instance;
}

void SetFileHandler::generate(){
    QDir dir;
    dir = QDir();
    if(!dir.exists(SETTINGSPATH+"/"))dir.mkdir(SETTINGSPATH+"/");
    if(!dir.exists(SETTINGSPATH+"/settings")){
        QFile settings(SETTINGSPATH+"/settings");
        settings.open(QIODevice::WriteOnly);
        QString setStr="[Firefox][/Firefox]\n[Chromium][/Chromium]\n[Opera][/Opera]";
        settings.write(setStr.toStdString().c_str());
        settings.close();
    }
}

bool SetFileHandler::readFfoxCustomPath(QString * path){

    if(!settingsFile->open(QIODevice::ReadOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isReadable()) {
        lastError="Settings File not readable!";
        settingsFile->close();
        return false;
    }

    QByteArray setBA= settingsFile->readAll();
    settingsFile->close();

    QString set(setBA);
    set.remove(0,set.indexOf("[Firefox]")+9);
    set.remove(set.indexOf("[/Firefox]"),set.length());
    *path = set;
    return true;



}

bool SetFileHandler::readChroCustomPath(QString * path){
    if(!settingsFile->open(QIODevice::ReadOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isReadable()) {
        lastError="Settings File not readable!";
        settingsFile->close();
        return false;
    }

    QByteArray setBA= settingsFile->readAll();
    settingsFile->close();

    QString set(setBA);
    set.remove(0,set.indexOf("[Chromium]")+10);
    set.remove(set.indexOf("[/Chromium]"),set.length());
    *path = set;

    return true;



}

bool SetFileHandler::readOperaCustomPath(QString * path){
    if(!settingsFile->open(QIODevice::ReadOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isReadable()) {
        lastError="Settings File not readable!";
        settingsFile->close();
        return false;
    }

    QByteArray setBA= settingsFile->readAll();
    settingsFile->close();

    QString set(setBA);
    set.remove(0,set.indexOf("[Opera]")+7);
    set.remove(set.indexOf("[/Opera]"),set.length());
    *path = set;

    return true;



}

bool SetFileHandler::writeFfoxCustomPath(QString newPath){
    if(!settingsFile->open(QIODevice::ReadOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isReadable()) {
        lastError="Settings File not readable!";
        settingsFile->close();
        return false;
    }

    QByteArray setBA= settingsFile->readAll();
    settingsFile->close();

    QString set(setBA);
    int start = set.indexOf("[Firefox]");
    int end= set.indexOf("[/Firefox]");
    if(start!= -1 && end != -1){
        set.remove(start+9,end-(start+9));
        set.insert(start+9,newPath);
    }
    else {
        lastError="Unable to parse correctly firefox settings";
        return false;
    }

    if(!settingsFile->open(QIODevice::WriteOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isWritable()) {
        lastError="Settings File not writable!";
        settingsFile->close();
        return false;
    }

    settingsFile->write(set.toStdString().c_str());
    settingsFile->close();
    return true;


}
bool SetFileHandler::writeChroCustomPath(QString newPath){
    if(!settingsFile->open(QIODevice::ReadOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isReadable()) {
        lastError="Settings File not readable!";
        settingsFile->close();
        return false;
    }

    QByteArray setBA= settingsFile->readAll();
    settingsFile->close();
    QString set(setBA);

    int start = set.indexOf("[Chromium]");
    int end= set.indexOf("[/Chromium]");
    if(start!= -1 && end != -1){


        set.remove(start+10,end-(start+10));
        set.insert(start+10,newPath);


    }
    else {
        lastError="Unable to parse correctly Chromium settings";
        return false;
    }


    if(!settingsFile->open(QIODevice::WriteOnly)) {
        lastError="Settings File not open!";
        return false;
    }
    if(!settingsFile->isWritable()) {
        lastError="Settings File not writable!";
        settingsFile->close();
        return false;
    }

    settingsFile->write(set.toStdString().c_str());
    settingsFile->close();
    return true;


}
bool SetFileHandler::writeOperaCustomPath(QString newPath){
    if(!settingsFile->open(QIODevice::ReadOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isReadable()) {
        lastError="Settings File not readable!";
        settingsFile->close();
        return false;
    }

    QByteArray setBA= settingsFile->readAll();
    settingsFile->close();
    QString set(setBA);
    int start = set.indexOf("[Opera]");
    int end= set.indexOf("[/Opera]");
    if(start!= -1 && end != -1){


        set.remove(start+7,end-(start+7));
        set.insert(start+7,newPath);

    }
    else {
        lastError="Unable to parse correctly firefox settings";
        return false;
    }


    if(!settingsFile->open(QIODevice::WriteOnly)) {
        lastError="Settings File not open!";
        return false;
    }

    if(!settingsFile->isWritable()) {
        lastError="Settings File not writable!";
        settingsFile->close();
        return false;
    }

    settingsFile->write(set.toStdString().c_str());
    settingsFile->close();
    return true;

}

QString SetFileHandler::getLastError(){
    return lastError;
}

